package com.lzs.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzs.PO.Menu;
import com.lzs.PO.Page;
import com.lzs.Service.MenuService;
import com.lzs.util.Msg;

@Controller
@RequestMapping(value = "/menu")
public class MenuController {
	@Autowired
	private MenuService menuService;

	@RequestMapping(value = "/menu_list")
	public String menu_list(Model model, HttpServletRequest request, @RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize, String keyword) throws Exception {

		Page<Menu> page = this.menuService.findMenuPageList(keyword, pageSize, pageNum);

		model.addAttribute("page", page);

		return "/menu/menu_list";
	}

	@ResponseBody
	@RequestMapping(value = "/menudata_load")
	public Object loaddata(Integer roleid) {
		// 构造顶级节点
		Menu parent = new Menu();
		parent.setMenuid("0");
		// 调用方法递归查询权限数据
		this.menuService.queryChildMenus(parent, roleid);
		
		System.out.println(parent.getChildren());
		return parent.getChildren();
	}

	@ResponseBody
	@RequestMapping(value = "menu_check")
	public Msg checkMenu(String menuid) {

		Boolean b = this.menuService.countMenuById(menuid);

		if (b) {
			return Msg.success();
		} else {

			return Msg.fail();
		}
	}

	@ResponseBody
	@RequestMapping(value = "/menu_add_action")
	public Msg addMenuAction(String menuid, String name, String url, String pid) {
		try {

			Map<String, Object> map = new HashMap<String, Object>();

			map.put("menuid", menuid);

			map.put("name", name);

			map.put("url", url);

			map.put("pid", pid);

			this.menuService.addMenu(map);

			return Msg.success();
		} catch (Exception e) {

			e.printStackTrace();

			return Msg.fail();
		}
	}

	@RequestMapping(value = "/menu_update")
	public String updateMenu(String menuid, Model model) {

		Menu menu = this.menuService.selectMenuById(menuid);

		Menu fatherMenu = this.menuService.selectMenuById(menu.getPid());

		model.addAttribute("menu", menu);

		model.addAttribute("fatherMenu", fatherMenu);

		return "/menu/menu_update";
	}

	@ResponseBody
	@RequestMapping(value = "/menu_update_action")
	public Msg updateMenuAction( String menuid, String name, String url, String pid) {
		try {
            Map<String,Object>map = new HashMap<String,Object>();
            
            map.put("menuid", menuid);
            
            map.put("name", name);
            
            map.put("pid", pid);
            
            map.put("url", url);
           
            this.menuService.updateMenu(map);
			

			return Msg.success();

		} catch (Exception e) {

			e.printStackTrace();

			return Msg.fail();

		}

	}

	@ResponseBody
     @RequestMapping(value = "/menu_delete")
     public Msg deleteMenu(String menuid) {
    	 
    	 try {
    		 this.menuService.deleteMenu(menuid);
    		 
    		 return Msg.success();
    	 }catch(Exception e) {
    		 
    		 e.printStackTrace();
    		 
    		 return Msg.fail();
    	 }
     }

	@RequestMapping(value = "/menu_add")
	public String addMenu() {

		return "/menu/menu_add";
	}

}
